var settings = app.getSettings();
var state = app.getState();
var uid = state.user_id;
var pages = ["coin.html","market.html","me.html","chat.html"]; // "kgmain.html" 移除矿机，有性能问题
var subStyle = {
	top: '45px',
	bottom: '50px'
};
var objSub = [];
for(var i = 0; i < pages.length; i++) {
	//创建多个窗口
	var sub = {
		url: pages[i],
		id: pages[i],
		styles: subStyle
	};
	objSub.push(sub);
}
mui.init({
	swipeBack: false,
	//beforeback: back,
	subpages: objSub
});
//创建窗体对象，把子窗口依次放入
mui.plusReady(function() {
	//if(!uid) {
	//location.href = 'login.html';
	//plus.runtime.restart();
	//}

	//1.获取到当前显示窗口
	/*
	var obj = plus.webview.currentWebview();

	for(var i = 0; i < pages.length; i++) {
		//创建多个窗口
		var sub = plus.webview.create(pages[i], pages[i], subStyle);
		sub.hide(); //隐藏窗口
		//加入到当前窗口，成为子窗口
		obj.append(sub);
	}
	//plus.webview.create(pages[0], pages[0], subStyle);
	plus.webview.show(pages[0], pages[0], subStyle);*/
	var backButtonPress = 0;
	mui.back = function() {
		if(showMenu) {
			//菜单处于显示状态，返回键应该先关闭菜单,阻止主窗口执行mui.back逻辑；
			closeMenu();
			return false;
		} else {
			//菜单处于隐藏状态，执行返回时，要先close菜单页面，然后继续执行mui.back逻辑关闭主窗口；
			menu.close('none');
		}
		backButtonPress++;
		if(backButtonPress > 1) {
			plus.runtime.quit();
		} else {
			plus.nativeUI.toast('再按一次退出应用');
		}
		setTimeout(function() {
			backButtonPress = 0;
		}, 1000);
	}
	var mainWin = plus.webview.currentWebview();
	//console.log(mainWin.id);
	localStorage.setItem('mainpage', mainWin.id);
	//var activeTab = pages[0];
	localStorage.setItem('defaultTab', 'chat.html');

	function switchpage(_event, targetTab) {
		var activeTab = localStorage.getItem('defaultTab');
		var activeObj = document.getElementById(activeTab);
		var targetObj = document.getElementById(targetTab);
		plus.webview.hide(activeTab);
		//显示目标选项卡
		//plus.webview.show(targetTab, "slide-in-right", 300);
		//console.log(targetTab);
		plus.webview.show(targetTab, "fade-in", 300);
		console.log(targetTab);
		title.innerHTML = document.getElementById('label' + targetTab).innerHTML;

		//switchPage("tap", activeTab, targetTab);
		//更改当前活跃的选项卡
		if(_event == "switch") {
			// 底部选项
			activeObj.classList.remove("mui-active");
			targetObj.classList.add("mui-active");
		}
		//activeTab = targetTab;

		localStorage.setItem('defaultTab', targetTab);
		if(activeTab == 'chat.html') {
			var chatWin = plus.webview.getWebviewById('chat.html');
			//console.log(JSON.stringify(chatWin));
			mui.fire(chatWin, 'refreshList', null);
		}

	}

	mui('.mui-bar-tab').on('tap', 'a', function(e) {
		var targetTab = this.getAttribute('id');
		var activeTab = localStorage.getItem('defaultTab');
		if(targetTab != activeTab) {
			//console.log(targetTab);
			//switchPage("tap", activeTab, targetTab);
			switchpage("tap", targetTab);

		}

	});
	//var defaultTab = localStorage.getItem('defaultTab');
	// 子页面滑动切换事件
	window.addEventListener("swipe_event", function(event) {
		// 获取方向以及索引
		var target = event.detail.target;
		switchpage("switch", target);

	});
});

var main, menu, mask = mui.createMask(_closeMenu),
	showMenu = false;

mui.plusReady(function() {
	main = plus.webview.currentWebview();
	//setTimeout的目的是等待窗体动画结束后，再执行create webview操作，避免资源竞争，导致窗口动画不流畅；
	setTimeout(function() {
		//侧滑菜单默认隐藏，这样可以节省内存；
		menu = mui.preload({
			id: 'plus-menu',
			url: 'pages/public/menu.html',
			styles: {
				top: '42px',
				right: '10px',
				width: '200px',
				height: '200px',
				overflow: 'hidden',
				zindex: 9997
			}
		});
	}, 300);

});
/**
 * 显示菜单菜单
 */
function openMenu() {
	//console.log(showMenu);
	if(!showMenu) {
		//侧滑菜单处于隐藏状态，则立即显示出来；
		//显示完毕后，根据不同动画效果移动窗体；
		menu.show();
		//显示遮罩
		mask.show();
		showMenu = true;
	}
}

function ocMenu() {
	if(!showMenu) {
		openMenu();
	} else {
		closeMenu();
	}
}
/**
 * 关闭侧滑菜单
 */
function closeMenu() {
	_closeMenu();
	//关闭遮罩
	mask.close();
}
/**
 * 关闭侧滑菜单（业务部分）
 */
function _closeMenu() {
	if(showMenu) {
		//关闭遮罩；
		main.setStyle({
			top: '0',
			transition: {
				duration: 150
			}
		});

		//等窗体动画结束后，隐藏菜单webview，节省资源；
		setTimeout(function() {
			menu.hide();
		}, 200);
		//改变标志位
		showMenu = false;
	}
}

document.querySelector('#main_popover').addEventListener('tap', ocMenu);
mui.menu = function() {
	if(showMenu) {
		closeMenu();
	} else {
		openMenu();
	}
}

window.addEventListener('closeMenu', function(e) {
	closeMenu();
});
